﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CountJobTags{
    /// <summary>
    /// Surprisingly, WPF does not have a native numeric up-down control
    /// </summary>
    public partial class NumericUpDown : UserControl {
        #region Events
        /// <summary>Raised when the value is changed</summary>
        public static readonly RoutedEvent ValueChangedEvent = EventManager.RegisterRoutedEvent("ValueChanged",
                                RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(NumericUpDown));
        
        /// <summary>Provide CLR accessors for the event</summary>
        public event RoutedEventHandler ValueChanged {
            add { AddHandler(ValueChangedEvent, value); }
            remove { RemoveHandler(ValueChangedEvent, value); }
        }

        /// <summary>This method raises the ValueChanged event</summary>
        void RaiseValueChangedEvent() {
            RoutedEventArgs newEventArgs = new RoutedEventArgs(NumericUpDown.ValueChangedEvent);
            RaiseEvent(newEventArgs);
        }
        #endregion

        #region Constructor
        /// <summary>
        /// Default constructor
        /// </summary>
        public NumericUpDown() {
            InitializeComponent();
            txtNum.Text = _Value.ToString();
        }
        #endregion

        #region Properties
        private int _Value = 0;
        /// <summary>
        /// Gets/sets the value user has assigned
        /// </summary>
        public int Value {
            get { return _Value; }
            set {
                _Value = value;
                txtNum.Text = value.ToString();
            }
        }

        /// <summary>
        /// How low the Dependency property is allowed to go
        /// </summary>
        public int Minimum {
            get { return (int)GetValue(MinimumProperty); }
            set { SetValue(MinimumProperty, value); }
        }
        /// <summary>
        /// Holds the specific value in a windows-defined storage area
        /// </summary>
        public static readonly DependencyProperty MinimumProperty =
                            DependencyProperty.Register("Minimum", typeof(int), typeof(NumericUpDown), new UIPropertyMetadata(0, MinimumChanged));

        /// <summary>
        /// This callback is linked to the dependency property and fired when the value is updated
        /// </summary>
        /// <param name="d">Minimum</param>
        /// <param name="e">Event args</param>
        static void MinimumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            NumericUpDown theControl = (NumericUpDown)d;
            if (theControl.Value < (int)e.NewValue) {
                theControl.Value = (int)e.NewValue;
            }
        }
        
        /// <summary>
        /// How high the control is allowed to go
        /// </summary>
        public int Maximum {
            get { return (int)GetValue(MaximumProperty); }
            set { SetValue(MaximumProperty, value); }
        }

        /// <summary>Holds the property value in a windows-defined location</summary>
        public static readonly DependencyProperty MaximumProperty =
            DependencyProperty.Register("Maximum", typeof(int), typeof(NumericUpDown), new UIPropertyMetadata(0, MaximumChanged));
        
        /// <summary>
        /// This callback is linked to the dependency property and fired when the value is updated
        /// </summary>
        /// <param name="d">The Maximum property</param>
        /// <param name="e">Event args</param>
        static void MaximumChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) {
            //Since the Dependency property is static, we must use the argument to identify which class instance to modify:
            NumericUpDown theControl = (NumericUpDown)d;
            //if the current value is out of range, fix:
            if (theControl.Value > (int)e.NewValue) {
                theControl.Value = (int)e.NewValue;
            }
        }
        #endregion

        /// <summary>
        /// Fires when user clicks the button to increase the displayed value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdUp_Click(object sender, RoutedEventArgs e) {
            if (_Value < Maximum) {
                Value++;
                RaiseValueChangedEvent();
            }
        }

        /// <summary>
        /// Fires when user clicks the button to decrease the displayed value
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cmdDown_Click(object sender, RoutedEventArgs e) {
            if (_Value >= Minimum) {
                Value--;
                RaiseValueChangedEvent();
            }
        }

        /// <summary>
        /// Fires when user types a value in the textbox
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtNum_TextChanged(object sender, TextChangedEventArgs e) {
            int newVal;
            if (!int.TryParse(txtNum.Text, out newVal)) {
                if (newVal >= Minimum) {
                    _Value = newVal;
                    RaiseValueChangedEvent();
                }
            }
            txtNum.Text = _Value.ToString();
        }
    }
}
